<template>
  <!--
      作者 yunding
      时间：2020-06-09
      描述：会员-等级管理-添加等级
  -->
  <el-drawer title="添加推广" :visible.sync="dialogVisible" @close='dialogFormVisible' :close-on-click-modal="false"
    :close-on-press-escape="false" size="700px">
    <el-form size="small" :model="form" ref="form">
      <el-form-item label="推广名称" :label-width="formLabelWidth" prop="title"
        :rules="[{ required: true, message: '请输入推广名称' }]" style="width: 500px;">
        <el-input v-model="form.title" placeholder="请输入推广名称"></el-input>
      </el-form-item>
      <el-form-item label="所属分类" :label-width="formLabelWidth" prop="tid"
        :rules="[{ required: true, message: '请选择所属分类' }]">
        <el-select v-model="form.tid" placeholder="请选择">
          <el-option v-for="(item, index) in cateList" :key="index" :label="item.tname" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="推广排序" :label-width="formLabelWidth" prop="sort"
        :rules="[{ required: true, message: '请输入推广排序' }]" style="width: 500px;">
        <el-input v-model="form.sort" placeholder="请输入推广排序"></el-input>
      </el-form-item>
      <el-form-item label="图片" :label-width="formLabelWidth" prop="pic" :rules="[{ required: true, message: '请选择图片' }]">
        <el-row>
          <el-button icon="el-icon-picture-outline" @click="openUpload()">选择图片</el-button>
          <div v-if="form.pic != ''" class="img">
            <img :src="form.pic" width="100" height="100" />
          </div>
        </el-row>
      </el-form-item>
      <el-form-item label="有效期" :label-width="formLabelWidth" prop="show_day"
        :rules="[{ required: true, message: '请选择有效期' }]" style="width: 500px;">
        <el-date-picker v-model="form.show_day" type="datetime" placeholder="选择日期时间" align="right">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="小程序appid" :label-width="formLabelWidth" prop="wx_appid"
        :rules="[{ required: true, message: '请输入小程序appid' }]" style="width: 500px;">
        <el-input v-model="form.wx_appid" placeholder="请输入小程序appid"></el-input>
      </el-form-item>
      <el-form-item label="小程序路径" :label-width="formLabelWidth" prop="wx_url"
        :rules="[{ required: true, message: '请输入小程序路径' }]" style="width: 500px;">
        <el-input v-model="form.wx_url" placeholder="请输入小程序路径"></el-input>
      </el-form-item>
    </el-form>
    <Upload v-if="isupload" :isupload="isupload" @returnImgs="returnImgsFunc">上传图片</Upload>
    <div style="width: 160px;margin: 0 auto;" class="dialog-footer">
      <el-button @click="dialogFormVisible">取 消</el-button>
      <el-button type="primary" @click="addTag()" :disabled="submit_loading">确 定</el-button>
    </div>
  </el-drawer>
</template>


<script>
import NewsApi from '@/api/news.js';
import Upload from '@/components/file/Upload';
export default {
  components: {
    /*上传组件*/
    Upload,
  },
  data() {
    return {
      form: {
        /*昵称*/
        title: '',
        tid: '',
        pic: '',
        show_day: '',
        wx_appid: '',
        wx_url: '',
        sort:'',
      },
      /*左边长度*/
      formLabelWidth: '120px',
      /*是否显示*/
      dialogVisible: false,
      /*是否正在提交*/
      submit_loading: false,
      isupload: false,
      cateList: [],
    };
  },
  props: ['open_edit', 'form'],
  created() {
    this.dialogVisible = this.open_edit;
    this.getData()
  },
  methods: {
    getData() {
      let self = this
      NewsApi.type_index({}, true).then(data => {
        self.cateList = data.data
      })
    },
     /*上传*/
     openUpload() {
      this.isupload = true;
    },
    returnImgsFunc(e) {
      if (e != null && e.length > 0) {
        this.form.pic = e[0].file_path;
      }
      this.isupload = false;
    },
    /*添加等级*/
    addTag() {
      let self = this;
      let params = this.form;
      self.$refs.form.validate((valid) => {
        if (valid) {
          self.submit_loading = true;
          NewsApi.plugs_edit(params, true).then(data => {
            self.submit_loading = false;
            self.$message({
              message: data.msg,
              type: 'success'
            });
            self.dialogFormVisible(true);
          })
            .catch(error => {
              self.submit_loading = false;
            });
        }
      });
    },
    /*关闭弹窗*/
    dialogFormVisible(e) {
      if (e) {
        this.$emit('closeDialog', {
          type: 'success',
          openDialog: false
        })
      } else {
        this.$emit('closeDialog', {
          type: 'error',
          openDialog: false
        })
      }
    }

  }
};
</script>

<style></style>
